package cn.jg.service.sys.impl;

import cn.jg.framework.auth.session.SessionManager;
import cn.jg.model.sys.Company;
import cn.jg.dao.repository.sys.command.CompanyCommandMapper;
import cn.jg.dao.repository.sys.query.CompanyQueryMapper;
import cn.jg.service.sys.CompanyService;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * Created by admin on 2023/06/06.
 */
@Slf4j
@Service
@AllArgsConstructor
public class CompanyServiceImpl implements CompanyService {

    private final CompanyQueryMapper companyQueryMapper;
    private final CompanyCommandMapper companyCommandMapper;

    @Override
    public Optional<Company> findById(@NonNull final Integer id) {
        Optional<Company> companyPossible = companyQueryMapper.findById(id);
        return companyPossible;
    }

    @Override
    public boolean exists(@NonNull final Integer id) {
        Optional<Company> companyPossible = companyQueryMapper.findById(id);
        return companyPossible.isPresent();
    }

    @Override
    public List<Company> findAll(final Integer pageNum, final Integer pageSize) {
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Company> companyList = companyQueryMapper.findAll();
        return companyList;
    }

    @Override
    public Company save(@NonNull final Company company) {
        SessionManager.packingCreateInfo(company);
        companyCommandMapper.save(company);
        return company;
    }

    @Override
    public Company update(@NonNull final Company company) {
        SessionManager.packingUpdateInfo(company);
        companyCommandMapper.update(company);
        return company;
    }

    @Override
    public void delete(Integer id) {
        companyCommandMapper.deleteById(id);
    }
}
